package meijuli.com.join51.business.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.youngfeng.architecture.image.RoundedCornersTransformation;

import meijuli.com.join51.business.Confirm;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.PictureDetailActivity;

import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 消息页面适配器
 *
 * @author wenqin 2017-07-20 15:13
 */

public class UserMsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int ITEM_CLICKABLE = 1;
    private static final int ITEM_TEXT = 2;
    private static final int ITEM_PIC = 3;

    private Context mContext;
    private List<Confirm> data;
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;

    public UserMsgAdapter(Context context, List<Confirm> data) {
        mContext = context;
        this.data = data;
        mInflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ITEM_CLICKABLE:
                return new ClickableItem(mInflater.inflate(R.layout.adapter_msg_clickable, parent, false));
            case ITEM_PIC:
                return new PicItem(mInflater.inflate(R.layout.adapter_msg_pic, parent, false));
            case ITEM_TEXT:
                return new TextItem(mInflater.inflate(R.layout.adapter_msg_text, parent, false));
            default:

                break;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Confirm confirm = data.get(position);
        if (holder instanceof ClickableItem) {
            ClickableItem clickableItem = (ClickableItem) holder;
            clickableItem.mTitle.setText(confirm.getTitle());

            if (position == 0) {
                Glide.with(mContext)
                        .load(confirm.getContent())
                        .error(R.mipmap.default_head)
                        .into(clickableItem.mHeadIcon);

                clickableItem.mHeadIcon.setVisibility(View.VISIBLE);
                clickableItem.mRightText.setVisibility(View.GONE);

                clickableItem.mHeadIcon.setOnClickListener(new PicClickListener(mContext,
                        confirm.getContent()));
            } else {
                clickableItem.mHeadIcon.setVisibility(View.GONE);
                clickableItem.mRightText.setVisibility(View.VISIBLE);

                clickableItem.mRightText.setText(confirm.getContent());
            }

        } else if (holder instanceof TextItem) {
            // 纯文字
            TextItem textItem = (TextItem) holder;
            textItem.mRightText.setText(
                    TextUtils.isEmpty(confirm.getContent()) ? "无" : confirm.getContent());
            textItem.mTitle.setText(confirm.getTitle());
        } else if (holder instanceof PicItem) {
            // 包含图片
            PicItem picItem = (PicItem) holder;
            picItem.mTitle.setText(confirm.getTitle());
            String content = confirm.getContent();

            // 无图片处理
            picItem.mTips.setVisibility(View.GONE);
            if (TextUtils.isEmpty(content)) {
                picItem.mTips.setVisibility(View.VISIBLE);
                return;
            }

            // 有图片加载图片
            String[] paths = content.split(",");
            if (paths.length >= 1) {
                Glide.with(mContext).load(paths[0]).error(R.mipmap.img_default)
                        .transform(new CenterCrop(mContext), new RoundedCornersTransformation(mContext, 10))
                        .into(picItem.mLeftIcon);
                picItem.mLeftIcon.setOnClickListener(new PicClickListener(mContext, paths[0]));
                picItem.mLeftIcon.setVisibility(View.VISIBLE);
            }
            if (paths.length == 2) {
                Glide.with(mContext).load(paths[1]).error(R.mipmap.img_default)
                        .transform(new CenterCrop(mContext), new RoundedCornersTransformation(mContext, 10))
                        .into(picItem.mRightIcon);
                picItem.mRightIcon.setOnClickListener(new PicClickListener(mContext, paths[1]));
                picItem.mRightIcon.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position <= 1) {
            return ITEM_CLICKABLE;
        } else {
            if (data.get(position).isContent()) {
                return ITEM_TEXT;
            } else {
                return ITEM_PIC;
            }
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class ClickableItem extends RecyclerView.ViewHolder {
        TextView mTitle;
        CircleImageView mHeadIcon;
        TextView mRightText;

        ClickableItem(View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.title);
            mHeadIcon = itemView.findViewById(R.id.head_icon);
            mRightText = itemView.findViewById(R.id.right_text);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    class TextItem extends RecyclerView.ViewHolder {
        TextView mTitle;
        TextView mRightText;

        TextItem(View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.title);
            mRightText = itemView.findViewById(R.id.right_text);
        }
    }

    class PicItem extends RecyclerView.ViewHolder {
        TextView mTitle;
        ImageView mLeftIcon;
        ImageView mRightIcon;
        TextView mTips;

        PicItem(View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.title);
            mLeftIcon = itemView.findViewById(R.id.left_icon);
            mRightIcon = itemView.findViewById(R.id.right_icon);
            mTips = itemView.findViewById(R.id.tips);
        }
    }

    private class PicClickListener implements View.OnClickListener {

        private String picPath;
        private Activity mContext;

        PicClickListener(Context context, String picPath) {
            mContext = (Activity) context;
            this.picPath = picPath;
        }

        @Override
        public void onClick(View v) {
            PictureDetailActivity.start(mContext, picPath);
        }
    }

    public interface OnItemClickListener{

        void onItemClick(int position);
    }
}


